package org.sn.jdish.interview.thoughtworks.purchase;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品数据处理
 */
public class MerchandiseDao {

	private static final String filePath = Class.class.getClass().getResource("/").getPath();
	private static final String newMerchandiseFile = filePath + "new.txt";
	private static final String purchaseMerchandiseFile = filePath + "purchase.txt";
	private static final String salesMerchandiseFile = filePath + "sales.txt";
	private static final String outputFile = filePath + "output.txt";

	/**
	 * 数据文件初始化
	 * 
	 * @throws Exception
	 */
	public static void init() throws Exception {
		File f1 = new File(newMerchandiseFile);
		if (!f1.exists()) {
			f1.createNewFile();
		}
		File f2 = new File(purchaseMerchandiseFile);
		if (!f2.exists()) {
			f2.createNewFile();
		}
		File f3 = new File(salesMerchandiseFile);
		if (!f3.exists()) {
			f3.createNewFile();
		}
		File f4 = new File(outputFile);
		if (!f4.exists()) {
			f4.createNewFile();
		}
	}

	/**
	 * 是否重复引进商品
	 * 
	 * @param merchandise
	 * @return
	 * @throws Exception
	 */
	public static boolean noNewMerchandise(Merchandise merchandise) throws Exception {
		FileReader fr = new FileReader(newMerchandiseFile);
		BufferedReader br = new BufferedReader(fr);
		String line = "";
		String[] newMerchandiseInfos = null;
		while ((line = br.readLine()) != null) {
			newMerchandiseInfos = line.split("\\s");
			if (merchandise.getBarCode().equals(newMerchandiseInfos[1])) {// 重复录入数据
				System.out.println("Barcode " + merchandise.getBarCode() + " already exist.");
				return false;
			}
		}
		br.close();
		fr.close();
		return true;
	}

	/**
	 * 是否重复录入采购
	 * 
	 * @param merchandise
	 * @return
	 * @throws Exception
	 */
	public static boolean noPurchaseMerchandise(Merchandise merchandise) throws Exception {
		FileReader fr = new FileReader(purchaseMerchandiseFile);
		BufferedReader br = new BufferedReader(fr);
		String line = "";
		String[] newMerchandiseInfos = null;
		while ((line = br.readLine()) != null) {
			newMerchandiseInfos = line.split("\\s");
			if (merchandise.getPurchaseTime().equals(newMerchandiseInfos[0])
					&& merchandise.getBarCode().equals(newMerchandiseInfos[1])) {// 重复录入数据
				return false;
			}
		}
		br.close();
		fr.close();
		return true;
	}

	/**
	 * 是否重复录入销售
	 * 
	 * @param merchandise
	 * @return
	 * @throws Exception
	 */
	public static boolean noSalesMerchandise(Merchandise merchandise) throws Exception {
		FileReader fr = new FileReader(salesMerchandiseFile);
		BufferedReader br = new BufferedReader(fr);
		String line = "";
		String[] newMerchandiseInfos = null;
		while ((line = br.readLine()) != null) {
			newMerchandiseInfos = line.split("\\s");
			if (merchandise.getSalesTime().equals(newMerchandiseInfos[0])
					&& merchandise.getBarCode().equals(newMerchandiseInfos[1])) {// 重复录入数据
				return false;
			}
		}
		br.close();
		fr.close();
		return true;
	}

	/**
	 * 保存引入商品
	 * 
	 * @param merchandise
	 * @throws Exception
	 */
	public static void saveNewMerchandise(Merchandise merchandise) throws Exception {
		System.out.println("NEW");
		System.out.println(Class.class.getClass().getResource("/").getPath());
		// 引进时间 商品编号 商品名称 售价
		File f = new File(newMerchandiseFile);
		FileWriter fw = new FileWriter(f, true);
		PrintWriter pw = new PrintWriter(fw);
		pw.println(merchandise.getNewTime() + " " + merchandise.getBarCode() + " " + merchandise.getName() + " "
				+ merchandise.getPrice());
		pw.flush();
		pw.close();
		fw.close();
	}

	/**
	 * 获取引入商品列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public static List<Merchandise> getNewMerchandiseList() throws Exception {
		List<Merchandise> newMerchandiseList = new ArrayList<Merchandise>();
		FileReader fr = new FileReader(newMerchandiseFile);
		BufferedReader br = new BufferedReader(fr);
		String line = "";
		String[] newMerchandiseInfos = null;
		while ((line = br.readLine()) != null) {
			newMerchandiseInfos = line.split("\\s");
			Merchandise newMerchandise = new Merchandise();
			newMerchandise.setNewTime(newMerchandiseInfos[0]);
			newMerchandise.setBarCode(newMerchandiseInfos[1]);
			newMerchandise.setName(newMerchandiseInfos[2]);
			newMerchandise.setPrice(Double.parseDouble(newMerchandiseInfos[3]));
			newMerchandiseList.add(newMerchandise);
		}
		br.close();
		fr.close();
		return newMerchandiseList;
	}

	/**
	 * 获取引入商品
	 * 
	 * @param barCode
	 * @return
	 * @throws Exception
	 */
	public static Merchandise getNewMerchandise(String barCode) throws Exception {
		List<Merchandise> newMerchandiseList = getNewMerchandiseList();
		for (Merchandise newMerchandise : newMerchandiseList) {
			if (newMerchandise.getBarCode().equals(barCode)) {
				return newMerchandise;
			}
		}
		return null;
	}

	/**
	 * 保存采购商品
	 * 
	 * @param barCode
	 * @return
	 * @throws Exception
	 */
	public static void savePurchaseMerchandise(Merchandise merchandise) throws Exception {
		System.out.println("PURCHASE");
		// 采购时间 商品编号 采购数量 采购价
		File f = new File(purchaseMerchandiseFile);
		if (!f.exists()) {
			f.createNewFile();
		}
		FileWriter fw = new FileWriter(f, true);
		PrintWriter pw = new PrintWriter(fw);
		pw.println(merchandise.getPurchaseTime() + " " + merchandise.getBarCode() + " " + merchandise.getPurchaseNum()
				+ " " + merchandise.getPurchasePrice());
		pw.flush();
		pw.close();
		fw.close();
	}

	/**
	 * 获取采购商品列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public static List<Merchandise> getPurchaseMerchandiseList() throws Exception {
		List<Merchandise> purchaseMerchandiseList = new ArrayList<Merchandise>();
		FileReader fr = new FileReader(purchaseMerchandiseFile);
		BufferedReader br = new BufferedReader(fr);
		String line = "";
		String[] newMerchandiseInfos = null;
		while ((line = br.readLine()) != null) {
			newMerchandiseInfos = line.split("\\s");
			Merchandise purchaseMerchandise = new Merchandise();
			purchaseMerchandise.setPurchaseTime(newMerchandiseInfos[0]);
			purchaseMerchandise.setBarCode(newMerchandiseInfos[1]);
			purchaseMerchandise.setPurchaseNum(Integer.parseInt(newMerchandiseInfos[2]));
			purchaseMerchandise.setPurchasePrice(Double.parseDouble(newMerchandiseInfos[3]));
			purchaseMerchandiseList.add(purchaseMerchandise);
		}
		br.close();
		fr.close();
		return purchaseMerchandiseList;
	}

	/**
	 * 保存销售商品
	 * 
	 * @param merchandise
	 * @throws Exception
	 */
	public static void saveSalesMerchandise(Merchandise merchandise) throws Exception {
		System.out.println("SALES");
		// 销售时间 商品编号 销售数量
		File f = new File(salesMerchandiseFile);
		if (!f.exists()) {
			f.createNewFile();
		}
		FileWriter fw = new FileWriter(f, true);
		PrintWriter pw = new PrintWriter(fw);
		pw.println(merchandise.getSalesTime() + " " + merchandise.getBarCode() + " " + merchandise.getSalesNum());
		pw.flush();
		pw.close();
		fw.close();
	}

	/**
	 * 获取销售商品列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public static List<Merchandise> getSalesMerchandiseList() throws Exception {
		List<Merchandise> salesMerchandiseList = new ArrayList<Merchandise>();
		FileReader fr = new FileReader(salesMerchandiseFile);
		BufferedReader br = new BufferedReader(fr);
		String line = "";
		String[] newMerchandiseInfos = null;
		while ((line = br.readLine()) != null) {
			newMerchandiseInfos = line.split("\\s");
			Merchandise salesMerchandise = new Merchandise();
			salesMerchandise.setSalesTime(newMerchandiseInfos[0]);
			salesMerchandise.setBarCode(newMerchandiseInfos[1]);
			salesMerchandise.setSalesNum(Integer.parseInt(newMerchandiseInfos[2]));
			salesMerchandiseList.add(salesMerchandise);
		}
		br.close();
		fr.close();
		return salesMerchandiseList;
	}

	/**
	 * 保存报表内容
	 * 
	 * @param output
	 * @throws Exception
	 */
	public static void saveReport(String output) throws Exception {
		File f = new File(outputFile);
		FileWriter fw = new FileWriter(f, false);
		PrintWriter pw = new PrintWriter(fw);
		pw.println(output);
		pw.flush();
		pw.close();
		fw.close();
		System.out.println("报表输出路径：" + outputFile);
	}

}
